package pages.menuPage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

import pages.masterPage.MasterPage;
import pages.report.ReportPage;
import testSpring.ConstructorMessage;
import testSpring.Greet;

public class MenuPage extends MasterPage
{	
//	@Autowired
//	private ApplicationContext applicationContext;
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private Greet greet;
	
	public MenuPage() 
	{
		connectDB();
//		runSpring();
		runSpring2();
		createButtons();
	}
	
	private void createButtons() 
	{
		Link<ReportPage> reportLink = new Link<ReportPage>("reportLink") 
		{
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() 
			{
				setResponsePage(ReportPage.class);
			}
		};
		add(reportLink);
		
	}

	private void connectDB()
	{
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager
					.getConnection("jdbc:oracle:thin:SYSTEM/Test12345@localhost:1522/HappyWork");
//			"jdbc:oracle:thin:SYSTEM/Test12345@VRNNB-PRC054.t-systems.ru:1522/HappyWork"
//			jdbc:oracle::[username/password]@[//]host_name[:port][/XE]
			stmt = con.createStatement();
			rs = stmt.executeQuery("SELECT * FROM HELP");
			while (rs.next()) {
				System.out.print(rs.getString(1) + "\t");
				System.out.println(rs.getString(2));
			}
		} 
		catch (ClassNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void runSpring()
	{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
        
        ConstructorMessage message =  (ConstructorMessage) applicationContext.getBean("message");
        System.out.println("!!!!!!");
	}
	
	private void runSpring2()
	{
//        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("*/applicationContext.xml"));

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Greet greetingService = (Greet) applicationContext.getBean("greet");

        greetingService.sayGreet();
		
		
        System.out.println("!!!!!!");
        
//        greet.sayGreet();
	}

}
